#!/usr/bin/env bash
#
# Copyright 2018 The Kubernetes Authors All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Set PROGram name
PROG=${0##*/}
########################################################################
#+
#+ NAME
#+     $PROG - Kubernetes Release Notification Tool
#+
#+ SYNOPSIS
#+     $PROG  <version> [--nomock] [--mailto=<user>,...]
#+                     [--security_layer=/path/to/pointer/to/script]
#+     $PROG  [--helpshort|--usage|-?]
#+     $PROG  [--help|-man]
#+
#+ DESCRIPTION
#+     Kubernetes releases produced on GCB are not able to send email
#+     notifications.  Use this helper tool to quickly send out a standard
#+     release notification after a release.  You can also see from the logs
#+     on GCB exactly the command to issue to send a notification following
#+     a GCB-based release.
#+
#+ OPTIONS
#+     [--nomock]                - Notifications to lists
#+     [--security_layer=]       - A file containing a path to a script to
#+                                 source/include:
#+                                 FLAGS_security_layer=/path/to/script
#+     [--mailto=]               - Useful for sending to self in --nomock mode
#+     [--help | -man]           - display man page for this script
#+     [--usage | -?]            - display in-line usage
#+
#+ EXAMPLES
#+
#+ FILES
#+     gs://kubernetes-release{,-$USER,-gcb}/archive/<version/announcement.html
#+
#+ SEE ALSO
#+     common.sh                 - common function definitions
#+     gitlib.sh                 - git/jenkins function definitions
#+     releaselib.sh             - release/push-specific functions
#+
#+ BUGS/TODO
#+
########################################################################
# If NO ARGUMENTS should return usage, uncomment the following line:
usage=${1:-yes}

# Deal with OSX limitations out the gate for anyone that tries this there
BASE_ROOT=$(dirname $(readlink -e "$BASH_SOURCE" 2>&1)) \
 || BASE_ROOT="$BASH_SOURCE"
source $BASE_ROOT/lib/common.sh
source $TOOL_LIB_PATH/gitlib.sh
source $TOOL_LIB_PATH/releaselib.sh

# Validate command-line
common::argc_validate 1 || common::exit 1 "Exiting..."

# Set positional args
RELEASE_VERSION=${POSITIONAL_ARGV[0]}

###############################################################################
# MAIN
###############################################################################
# Default mode is a mocked release workflow
: ${FLAGS_nomock:=0}

##############################################################################
# Initialize logs
##############################################################################
# Initialize and save up to 10 (rotated logs)
if ((FLAGS_stage)); then
  LOGFILE=$TMPDIR/$PROG-stage.log
else
  LOGFILE=$TMPDIR/$PROG.log
fi
common::logfileinit $LOGFILE 10

# BEGIN script
common::timestamp begin

# Additional functionality
common::security_layer
security_layer::auth_check 1 || common::exit 1

common::set_cloud_binaries
release::set_globals

release::send_announcement || common::exit 1

common::timestamp end
